package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.group.network.express_plus_app.bean.MyResponse;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.http.GsonRequest;
import com.example.group.network.express_plus_app.http.MySingleton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WaitFinishShowDetailActivity extends AppCompatActivity implements View.OnClickListener {
    @BindView(R.id.show_courier_phone)
    TextView show_courier_phone;
    @BindView(R.id.show_user_phone)
    TextView show_user_phone;
    @BindView(R.id.show_express_type)
    TextView show_express_type;
    @BindView(R.id.show_estimated_price)
    TextView show_estimated_price;
    @BindView(R.id.show_stuff_detail)
    TextView show_stuff_detail;
    @BindView(R.id.show_delivery_mode)
    TextView show_delivery_mode;
    @BindView(R.id.show_express_detail_address)
    TextView show_express_detail_address;
    @BindView(R.id.show_delivery_address)
    TextView show_delivery_address;
    @BindView(R.id.show_user_leisure)
    TextView show_user_leisure;
    @BindView(R.id.show_order_id)
    TextView show_order_id;
    @BindView(R.id.show_order_create_time)
    TextView show_order_create_time;
    @BindView(R.id.show_receive_order_time)
    TextView show_receive_order_time;
    Order order;
    RequestQueue queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait_finish_show_detail);
        ButterKnife.bind(this);
        setActionBar("信息详细");
        queue= MySingleton.getInstance(getApplicationContext()).getRequestQueue();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        order=(Order) bundle.getSerializable("ORDER");
        show_courier_phone.setText(order.getCourierPhone());
        show_user_phone.setText(order.getUserPhone());
        show_express_type.setText(order.getExpressType());
        show_estimated_price.setText(String.valueOf(order.getEstimatedPrice()));
        show_stuff_detail.setText(order.getStuffDetail());
        show_delivery_mode.setText(order.getDeliveryMode());
        show_express_detail_address.setText(order.getExpressDetailAddress());
        show_delivery_address.setText(order.getDeliveryAddress());
        show_user_leisure.setText(order.getUserLeisure());
        show_order_id.setText(order.getOrderId());
        String[] split = order.getOrderCreateTime().split("\\.");
        Log.i("split",split[1]);
        show_order_create_time.setText(split[0]);
        show_receive_order_time.setText(order.getReceiveOrderTime());
        findViewById(R.id.btn_sure_receive_express).setOnClickListener(this);
    }

    public void setActionBar(String title){
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle(title);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        be_sure_receive_express(order.getOrderId());
    }

    public void be_sure_receive_express(String order_id){
        //获取token和phone
        HashMap<String, String> mInfoMap = MainApplication.getInstance().mInfoMap;
        String token = mInfoMap.get("token");
        String server_ip = getApplication().getString(R.string.server_ip);
        String url=server_ip+"express-order/order/customer-confirm-order?orderId="+order_id;
        HashMap<String, String> header = new HashMap<>();
        header.put("Authorization","Bearer "+token);
        GsonRequest<MyResponse> myResponseGsonRequest=new GsonRequest<>(url, Request.Method.POST, MyResponse.class, header, new Response.Listener<MyResponse>() {
            @Override
            public void onResponse(MyResponse response) {
                if (response.getOk()){
                    Toast.makeText(WaitFinishShowDetailActivity.this, order_id+"该订单已确认收货", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(WaitFinishShowDetailActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(WaitFinishShowDetailActivity.this, "请检查网络链接", Toast.LENGTH_SHORT).show();
            }
        });
        myResponseGsonRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(myResponseGsonRequest);
    }
}